<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class Album extends IndexController
{
    public function index()
    {


        $albumList = db('album')->where('use_id',input('session.user.id'))->order('createtime desc')->select();
        $this->assign('albumList', $albumList);



        return view('album/index');
    }



    public function addAlbum()
    {   

        // return json(input());exit;
        if (request()->isPost()) {
            $data = ['use_id' => input('session.user.id'), 'title' => input('title'), 'createtime' => date("Y-m-d H:i:s"), 'description' => input('description')];
            //动态
            $dongtai = ['use_id' => $data['use_id'],'dtime' => date("Y-m-d H:i:s"),'caozuo' => '创建了新相册'.$data['title']];
            db('dongtai')->insert($dongtai);


            $validate = validate('Album');
            if ($validate->check($data)) {
                if(db('album')->insert($data)){
                    return json('保存信息成功');
                }else{
                    return json('保存信息失败');
                }

            }
            return json($validate->getError());
 
        }
        return json('what do you want to do?');

    }


    public function image()
    {

        $album = db('album')->where('id',input('id'))->find();
        $this->assign('album', $album);
        if(!$album){
            return view('404/404');
        }

        $imgList = db('img')->where('alb_id',input('id'))->order('uptime desc')->paginate(16);
        $this->assign('imgList', $imgList);

        return view('album/image');
    }



    public function delalbum()
    {

        $id = input('id');
        $title = db('album')->field('title')->where('id',$id)->find();
        //动态
        $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '删除了相册'.$title['title']];
        db('dongtai')->insert($dongtai);

        $result = db('album')->where('id',$id)->delete();
        if($result){
            db('img')->where('alb_id',$id)->delete();
            return $this->success('删除相册成功','index/album/index');
        }else{
            return $this->error('删除失败了');
        }
    }


    public function editimg()
    {

        if (request()->isPost()) {
            $data = ['imgname' => input('imgname'), 'id' => input('id')];
            $validate = validate('Albumimg');
            if ($validate->check($data)) {
                $result = Db::name('img')->where('id',$data['id'])->update(['imgname'=>$data['imgname']]);
                if($result){
                    return json('保存信息成功');
                }else{
                    return json('保存信息失败');
                }

            }
            return json($validate->getError());
 
        }
        return json('what do you want to do?');
    }



    public function addimg()
    {

        $album = db('album')->where('use_id',input('session.user.id'))->select();
        if(!$album){
            return $this->error('请先创建相册');
        }
        $this->assign('albums', $album);

        return view('album/addimg');
    }


    public function doaddimg()
    {
        

        $file = request()->file('image');
        if(!$file){
            return $this->error('图片上传失败');
        }

        $info = $file->validate(['size'=>100000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $data = ['alb_id' => input('select'),'use_id' => input('session.user.id'),'uptime' => date("Y-m-d H:i:s"),'src'=>$info->getSaveName(),'imgname' => $info->getSaveName()];
            //上传成功
            if(Db::name('img')->where('alb_id','3')->insert($data)){
                //动态
                $title = db('album')->field('title')->where('id',$data['alb_id'])->find();
                $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '上传了新照片至'.$title['title']];
                db('dongtai')->insert($dongtai);

                return $this->success('图片上传成功');
            }else{
                return $this->error('图片上传失败');
            }
        }else{
            return $this->error($file->getError());
        }


    }



    public function delimg()
    {

        $id = input('id');
        $result = db('img')->where('id',$id)->delete();
        if($result){
            return json('删除图片成功');
        }else{
            return json('删除图片失败');
        }
    }



    

}